/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * RegistrarEleccion.java
 *
 * Created on 14/11/2010, 01:45:26 AM
 */

package zonasoft.Vistas.Funcionales;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.border.Border;
import zonasoft.Manejadores.ManejadorBaseDatos;
import zonasoft.Conceptos.Usuario;
import zonasoft.Manejadores.ManejadorUsuario;
import zonasoft.Utilidades.ErrorLog;
/**
 *
 * @author JORGE
 */
public class RegistrarUsuario extends javax.swing.JDialog {

    /** Creates new form RegistrarEleccion */
     public RegistrarUsuario(JFrame parent) {
        super(parent,true);
        initComponents();
        pack();
        Rectangle parentBounds = parent.getBounds();
        Dimension size = getSize();
        // Center in the parent
        int x = Math.max(0, parentBounds.x + (parentBounds.width - size.width) / 2);
        int y = Math.max(0, parentBounds.y + (parentBounds.height - size.height) / 2);
        setLocation(new Point(x, y));
    }
     Usuario us = null;
     ManejadorUsuario mu = new ManejadorUsuario();

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jXPanelElecciones1 = new org.jdesktop.swingx.JXPanel();
        jLabel8 = new javax.swing.JLabel();
        campoNombre = new javax.swing.JTextField();
        jLabel11 = new javax.swing.JLabel();
        campoId = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        contraseña = new javax.swing.JPasswordField();
        jLabel10 = new javax.swing.JLabel();
        contraseña1 = new javax.swing.JPasswordField();
        jLabel12 = new javax.swing.JLabel();
        campoFecha = new org.jdesktop.swingx.JXDatePicker();
        jPanel3 = new javax.swing.JPanel();
        botonCancelar = new javax.swing.JButton();
        botonGuardar = new javax.swing.JButton();
        jXTaskPaneContainer1 = new org.jdesktop.swingx.JXTaskPaneContainer();
        jLabel1 = new javax.swing.JLabel();

        setTitle("");

        jXPanelElecciones1.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED, java.awt.Color.gray, java.awt.Color.lightGray, java.awt.Color.black, java.awt.Color.darkGray), "Datos Personales", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Lucida Bright", 2, 18))); // NOI18N

        jLabel8.setFont(new java.awt.Font("Lucida Bright", 0, 12));
        jLabel8.setText("Contraseña");

        jLabel11.setFont(new java.awt.Font("Lucida Bright", 0, 12));
        jLabel11.setText("Identificacion");

        campoId.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                campoIdKeyTyped(evt);
            }
        });

        jLabel9.setFont(new java.awt.Font("Lucida Bright", 0, 12));
        jLabel9.setText("Nombre de Usuario");

        jLabel10.setFont(new java.awt.Font("Lucida Bright", 0, 12));
        jLabel10.setText("Confirma  Contraseña");

        jLabel12.setFont(new java.awt.Font("Lucida Bright", 0, 12));
        jLabel12.setText("Fecha de Registro");

        campoFecha.setEditable(false);

        jPanel3.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED, java.awt.Color.lightGray, java.awt.Color.lightGray, java.awt.Color.lightGray, java.awt.Color.gray));

        botonCancelar.setFont(new java.awt.Font("Lucida Bright", 3, 12));
        botonCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/zonasoft/Vistas/iconos/Delete-icon.png"))); // NOI18N
        botonCancelar.setText("Cancelar");
        botonCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonCancelarActionPerformed(evt);
            }
        });

        botonGuardar.setFont(new java.awt.Font("Lucida Bright", 3, 12)); // NOI18N
        botonGuardar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/zonasoft/Vistas/iconos/Floppy-icon.png"))); // NOI18N
        botonGuardar.setText("Guardar");
        botonGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonGuardarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(botonCancelar, javax.swing.GroupLayout.DEFAULT_SIZE, 127, Short.MAX_VALUE)
                    .addComponent(botonGuardar, javax.swing.GroupLayout.DEFAULT_SIZE, 127, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGap(12, 12, 12)
                .addComponent(botonGuardar)
                .addGap(18, 18, 18)
                .addComponent(botonCancelar)
                .addContainerGap(13, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jXPanelElecciones1Layout = new javax.swing.GroupLayout(jXPanelElecciones1);
        jXPanelElecciones1.setLayout(jXPanelElecciones1Layout);
        jXPanelElecciones1Layout.setHorizontalGroup(
            jXPanelElecciones1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jXPanelElecciones1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jXPanelElecciones1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel12)
                    .addComponent(jLabel8)
                    .addComponent(jLabel11))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jXPanelElecciones1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jXPanelElecciones1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(campoId)
                        .addComponent(campoFecha, javax.swing.GroupLayout.DEFAULT_SIZE, 118, Short.MAX_VALUE))
                    .addComponent(contraseña, javax.swing.GroupLayout.DEFAULT_SIZE, 118, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addGroup(jXPanelElecciones1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel9, javax.swing.GroupLayout.PREFERRED_SIZE, 122, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel10))
                .addGap(10, 10, 10)
                .addGroup(jXPanelElecciones1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(campoNombre, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(contraseña1, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jXPanelElecciones1Layout.setVerticalGroup(
            jXPanelElecciones1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jXPanelElecciones1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jXPanelElecciones1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jXPanelElecciones1Layout.createSequentialGroup()
                        .addGroup(jXPanelElecciones1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel12)
                            .addComponent(campoFecha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(jXPanelElecciones1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jXPanelElecciones1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(campoId, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel9)
                                .addComponent(campoNombre, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jLabel11))
                        .addGap(20, 20, 20)
                        .addGroup(jXPanelElecciones1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel8)
                            .addComponent(contraseña, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel10)
                            .addComponent(contraseña1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(jPanel3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );

        jLabel1.setFont(new java.awt.Font("Vani", 3, 18));
        jLabel1.setText("Registro de Usuarios del Programa");
        jXTaskPaneContainer1.add(jLabel1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jXTaskPaneContainer1, javax.swing.GroupLayout.DEFAULT_SIZE, 760, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addGap(28, 28, 28)
                .addComponent(jXPanelElecciones1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jXTaskPaneContainer1, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(34, 34, 34)
                .addComponent(jXPanelElecciones1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(26, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void botonGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonGuardarActionPerformed
        // TODO add your handling code here:
        ManejadorBaseDatos mb = ManejadorBaseDatos.getInstancia();
         if(campoId.getText().isEmpty()){
                JOptionPane.showMessageDialog(null, "el campo id Eleccion esta vacio", "error",JOptionPane.WARNING_MESSAGE);
            }else if(campoNombre.getText().isEmpty()) {
                JOptionPane.showMessageDialog(null, "el campo nombre esta vacio", "error",JOptionPane.WARNING_MESSAGE);
             }else if(contraseña.getPassword().toString().isEmpty()){
//             .getText().isEmpty()){
                JOptionPane.showMessageDialog(null," su contraseña  esta vacio", "error", JOptionPane.WARNING_MESSAGE);
             }else if(contraseña1.getPassword().toString().isEmpty()){
//             .getText().isEmpty()){
                 JOptionPane.showMessageDialog(null," su confirmacion de contraseña esta vacio", "error", JOptionPane.WARNING_MESSAGE);
//                } else if( contraseña.getText() == null ? contraseña1.getText() != null : !contraseña.getText().equals(contraseña1.getText())){
             } else if( contraseña.getPassword().toString() == null ? contraseña1.getPassword().toString() != null : contraseña.getPassword().toString().equals(contraseña1.getPassword().toString())){
                      JOptionPane.showMessageDialog(null," por favor confirme su contraseña", "error", JOptionPane.WARNING_MESSAGE);  
             }

             else{
        try {
                  mb.conectar();
            ManejadorUsuario.conm =mb.getConexion();

//            if(mu.getUsuario(campoId.getText())!=null){
                 if(ManejadorUsuario.getUsuario(campoId.getText())!=null){
                JOptionPane.showMessageDialog(null, "Este usuario ya esta registrado \n Intente Nuevamente", "error",JOptionPane.WARNING_MESSAGE);
                 limpiar();
                 }else{

                us = new Usuario();
                int id= Integer.parseInt(campoId.getText());
               us.setId(id);
               us.setLogin_usuario(campoNombre.getText());
//               us.setClave_usuario(contraseña.getText().toString());
                 us.setClave_usuario(contraseña.getText());
               us.setFecha_registro(campoFecha.getDate());

               int c= mu.crearUsuario(us);
               if(c==1)
               { limpiar();}
               else{
               if(c==0)
               {
                   campoId.requestFocus();
//                   campoId.setBackground(Color.red);
               }

               }
              
            }
        } catch (Exception ex) {
            
           ErrorLog.logger= Logger.getLogger(RegistrarUsuario.class.getName());
           ErrorLog.logger.log(Level.SEVERE, null, ex);
            
        }
         }
    }//GEN-LAST:event_botonGuardarActionPerformed

    private void botonCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonCancelarActionPerformed
        // TODO add your handling code here:
        int resp = JOptionPane.showConfirmDialog(this, "Seguro que deseas cerrar la Ventana ","Warning", JOptionPane.YES_NO_OPTION);
        if(resp == JOptionPane.YES_OPTION){
            this.dispose();
        }
}//GEN-LAST:event_botonCancelarActionPerformed

    private void campoIdKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_campoIdKeyTyped
        // TODO add your handling code here:
        validaNumeros(evt, campoId, 10);
    }//GEN-LAST:event_campoIdKeyTyped

     private void limpiar() {
    campoId.setText("");
    campoNombre.setText("");
     contraseña.setText("");
     contraseña1.setText("");
            }

       private void validaNumeros(java.awt.event.KeyEvent evt, JTextField campo, int numero) {
        int caracter = (int) evt.getKeyChar();
        char letra = ((char) caracter);

         int n = campo.getText().length() + 1;
         if(n > numero){evt.setKeyChar((char)KeyEvent.VK_CLEAR); campo.setToolTipText(("<html> <center> <h1> Advertencia </h1>"                  + "<h2 Style='color: red; font-style: italic;'> No puede superar la cantidad de caracteres permitidos </h2>"
                  + "<b>" + --n +" <br/> Es el maximo  para este campo </b> </center>"
                  + "<br/> Compruebe sus datos."));}

            //           solo numeros               retroceso       ctrl+c           ctrl + v          ctrl + x          supri                  -
        if (caracter >= 48 && caracter <= 57 || caracter == 8 || caracter == 3 || caracter == 22 || caracter == 24 || caracter == 27 || caracter == 45) {
        } else {
//        int numeropermitido = campo.getText().length();
            evt.setKeyChar((char) KeyEvent.VK_CLEAR);
            campo.setToolTipText(("<html> <center> <h1> Advertencia </h1>"
                    + "<h2 Style='color: red; font-style: italic;'> Ingrese numeros por favor </h2>"
                    + "<b> " + letra + "<br/> no es un numero </b> </center>"
                    + "<br/> Compruebe sus datos."));
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton botonCancelar;
    private javax.swing.JButton botonGuardar;
    private org.jdesktop.swingx.JXDatePicker campoFecha;
    private javax.swing.JTextField campoId;
    private javax.swing.JTextField campoNombre;
    private javax.swing.JPasswordField contraseña;
    private javax.swing.JPasswordField contraseña1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel3;
    private org.jdesktop.swingx.JXPanel jXPanelElecciones1;
    private org.jdesktop.swingx.JXTaskPaneContainer jXTaskPaneContainer1;
    // End of variables declaration//GEN-END:variables

}
